package com.jzlg.miniec.controller;

import com.jzlg.miniec.entity.Product;
import com.jzlg.miniec.entity.User;
import com.jzlg.miniec.service.ProductService;
import com.jzlg.miniec.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author StCat
 * @Date 2021/11/29 10:10
 */
@Controller
public class UserController {
    //注入依赖
    @Resource
    private UserService userService;
    //注入产品操作接口
    @Resource
    private ProductService productService;

    //1.到登录页
    @RequestMapping("/")
    public String giLogin(){
        return "pages/login";
    }

    //执行登录
    @RequestMapping("/dolog")
    public String doLogin(Map map, String username, String password){
       String info= userService.findUserByNameAndPass(username,password);
        //登录成功跳转到success
        if (info.equals("ok")){

            //传用户名到下一页
            map.put("username",username);

            //传所有商品到success页面
            List<Product> products=productService.findProducts();
            map.put("products",products);

            return "pages/success";
        }
    //传登录的错误信息的前端
        map.put("error",info);
       return "pages/login";
    }
    //2.到注册页
    @RequestMapping("/goreg")
    public  String goRegister(){

        return "pages/register";
    }

    //执行注册
    @RequestMapping("/doreg")
    public  String doRegister(User user, Map map){
        String info =userService.saveUser(user);

        map.put("info",info);
        return "pages/register";
    }
}
